package jcommon.chap10_io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.Writer;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

public class CopyingAndWriting {

	public static void main(String[] args) {
		System.out
				.println("---- Copying a stream, byte[], Reader, or Writer ----");
		testCopyingBetweenFlows();

		System.out.println("---- Copying files ----");
		testCopyingBetweenFiles();

		System.out.println("---- testWriteStringToFile ----");
		testWriteStringToFile();

		System.out.println("---- testWriteUrlContentToFile ----");
		testWriteUrlContentToFile();
	}

	private static void testCopyingBetweenFlows() {
		File tempFile = new File(classpath() + "testCopyingBetweenFlows.dat");
		Writer writer = null;
		InputStream inputStream = null;
		try {
			writer = new FileWriter(tempFile);
			inputStream = getTestDataIS();
			// Copying.class.getResourceAsStream("test.dat");
			IOUtils.copy(inputStream, writer);
		} catch (IOException e) {
			System.out.println("Error copying data");
		} finally {
			IOUtils.closeQuietly(writer);
			IOUtils.closeQuietly(inputStream);
			verifyAndDeleteResult(tempFile);
		}
	}

	private static void testCopyingBetweenFiles() {
		File srcFile = new File(getTestDataPath());
		File destFile = new File(classpath() + "testCopyingBetweenFiles.dat");
		try {
			FileUtils.copyFile(srcFile, destFile);
		} catch (IOException e) {
			throw new RuntimeException(e);
		} finally {
			verifyAndDeleteResult(destFile);
		}
	}
	
	private static void testWriteStringToFile() {
		String srcString = "Blah blah blah，这是测试";
		File destFile = new File(classpath() + "testWriteStringToFile.txt" );
		try {
			FileUtils.writeStringToFile( destFile, srcString, "gb2312" );
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			verifyAndDeleteResult(destFile);
		}

	}

	private static void testWriteUrlContentToFile() {
		URL srcUrl = null;
		File destFile = new File(classpath() + "times.html");
		try {
			srcUrl = new URL("http://10.60.80.208/index.php");
			FileUtils.copyURLToFile(srcUrl, destFile);
		} catch (MalformedURLException e) {
			throw new RuntimeException(e);
		} catch (IOException e) {
			throw new RuntimeException(e);
		} finally {
			verifyAndDeleteResult(destFile);
		}
	}

	// -------------- 工具方法 --------------
	private static void verifyAndDeleteResult(File file) {
		if (file == null || !file.exists() || file.isDirectory())
			return;
		System.out.println("-- 文件" + file.getAbsolutePath() + " --");
		String fileContent = null;
		try {
			fileContent = IOUtils.toString(new FileReader(file));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println(fileContent);
		file.delete();
	}

	private static InputStream getTestDataIS() {
		try {
			return new FileInputStream(getTestDataPath());
		} catch (FileNotFoundException e) {
			throw new RuntimeException(e);
		}
	}

	private static String getTestDataPath() {
		return classpath() + "test.dat";
	}

	private static String classpath() {
		return CopyingAndWriting.class.getResource("").getPath();
	}
}
